// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations
//
// Date: 03-21-2009
// Author: Miguel Angel Anton <maantonr@gmail.com>
// Notes: Transalation to Spanish. Changed date format. Modified to use css 

function show_calendar(str_target, str_datetime, str_path) {
   var arr_months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
      "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
   var week_days = ["D", "L", "M", "X", "J", "V", "S"];
   var n_weekstart = 1; // day week starts from (normally 0 or 1)

   var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
   var dt_prev_month = new Date(dt_datetime);
   dt_prev_month.setMonth(dt_datetime.getMonth()-1);
   var dt_next_month = new Date(dt_datetime);
   dt_next_month.setMonth(dt_datetime.getMonth()+1);
   var dt_firstday = new Date(dt_datetime);
   dt_firstday.setDate(1);
   dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
   var dt_lastday = new Date(dt_next_month);
   dt_lastday.setDate(0);
   
   
   // html generation (feel free to tune it for your particular application)
   // print calendar header
   var str_buffer = new String (
      "<html>\n"+
      "<head>\n"+
      "\t<title>Calendario</title>\n"+
	  "\t<link href='"+str_path+"/css/calendar.css' rel='stylesheet' type='text/css' media='screen'></link>\n" +
	  "\t<script type='text/javascript' src='"+str_path+"/js/calendar.js'></script>\n"+
      "</head>\n"+
      "<body>\n"+
      "\t<script type=\"text/javascript\">\n"+
	  "\t\tfunction selectDate(d) {\n"+
      "\t\t\topener.document.getElementById('"+str_target+"').value=d;\n" +
      "\t\t\twindow.close();\n"+
	  "\t\t}\n"+
	  "\t\tfunction changeMonth(d) {\n"+
      "\t\t\twindow.opener.show_calendar('"+str_target+"', d, '"+str_path+"');\n" +
	  "\t\t}\n"+
	  "\t</script>\n"+
      "\t<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
      "\t\t<tr><td>\n"+
      "\t\t\t<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
      "\t\t\t\t<tr>\n"+
      "\t\t\t\t\t<td class='nameofMonth'><a href=\"javascript:changeMonth('"+ dt2dtstr(dt_prev_month)+"');\">"+
      "<img src=\"" + str_path + "/css/images/prev.png\" width=\"16\" height=\"16\" border=\"0\""+
      " alt=\"Mes anterior\"></a></td>\n"+
      "\t\t\t\t\t<td class='nameofMonth' colspan=\"5\">"+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</td>\n"+
      "\t\t\t\t\t<td class='nameofMonth'><a href=\"javascript:changeMonth('"+dt2dtstr(dt_next_month)+"');\">"+
      "<img src=\"" + str_path + "/css/images/next.png\" width=\"16\" height=\"16\" border=\"0\""+
      " alt=\"Proximo mes\"></a></td>\n" +
      "\t\t\t\t</tr>\n"
   );
   
   var dt_current_day = new Date(dt_firstday);
   // print weekdays titles
   str_buffer += "\t\t\t\t<tr>\n";
   for (var n=0; n<7; n++)
      str_buffer += "\t\t\t\t\t<td class='daysOfWeek'>" + week_days[(n_weekstart+n)%7]+"</td>\n";
   // print calendar table
   str_buffer += "\t\t\t\t</tr>\n";
   while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
      dt_current_day.getMonth() == dt_firstday.getMonth()) {
      // print row heder
      str_buffer += "\t\t\t\t<tr>\n";
      for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
            if (dt_current_day.getDate() == dt_datetime.getDate() &&
               dt_current_day.getMonth() == dt_datetime.getMonth())
               // print current date
               str_buffer += "\t\t\t\t\t<td class='selected'>";
            else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6) {
                if (dt_current_day.getMonth() == dt_datetime.getMonth()) {
                    // Fin de semana del mes actual
                   str_buffer += "\t\t\t\t\t<td class='weekEnd'>";
                } else {
                    // Fin de semana de otros meses
                   str_buffer += "\t\t\t\t\t<td class='otherWeekEnd'>";
                }
            }
            else {
                if (dt_current_day.getMonth() == dt_datetime.getMonth()) {
                    // Dia laborable del mes actual
                   str_buffer += "\t\t\t\t\t<td>";
                } else {
                    // Dia Laborable de otros meses
                   str_buffer += "\t\t\t\t\t<td class='otherMonth'>";
                }
            }

            str_buffer += "<a href=\"javascript:selectDate('"+dt2dtstr(dt_current_day)+"');\">";
            str_buffer += dt_current_day.getDate()+"</a></td>\n";
            dt_current_day.setDate(dt_current_day.getDate()+1);
      }
      // print row footer
      str_buffer += "\t\t\t\t</tr>\n";
   }

   // print calendar footer
   str_buffer +=
      "\t\t\t</table>\n" +
      "\t\t</tr></td>\n"+
      "\t</table>\n" +
      "</body>\n" +
      "</html>\n";
   var vWinCal = window.open("", "Calendario", "width=200,height=180,status=no,resizable=no,top=200,left=200");
   vWinCal.opener = self;
   var cal_doc = vWinCal.document;
   cal_doc.write (str_buffer);
   cal_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
   var re_date = /^(\d+)-(\d+)-(\d+)\s*$/;
   if (!re_date.exec(str_datetime))
      return alert("Formato de fecha incorrecta: "+ str_datetime);
   return (new Date (RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
   var m = dt_datetime.getMonth() + 1;
   var d = dt_datetime.getDate();
   var str = new String(dt_datetime.getFullYear()+"-"+
             (m < 10? "0"+m: m)+"-"+
             (d < 10? "0"+d: d));
   return (str);
}

function muestrCalendario(name, path) {
	show_calendar(name, document.getElementById(name).value, path);
}

